docker create

1 1概述

docker create 命令用于新建一个容器, 但是不会运行它。

2 语法大纲

$ docker create  --help

Usage:  docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

Create a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --domainname string              Container NIS domain name
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --gpus gpu-request               GPU devices to add to the container ('all' to pass all GPUs)
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before creating ("always"|"missing"|"never") (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container

3 参数简介

3.1 Create 命令与容器运行模式相关的选项

参数 说明
-a,--attach=[] 是否绑定到标准输入、输出和错误。
-d,--detach=true\false 是否在后台运行容器,默认为否。
--detach-keys="" 从 attach 模式退出的快捷键。
--entrypoint="" 镜像存储在入口命令时,会覆盖为新的命令。
--expose=[] 指定容器会暴露出来的端口或端口范围。
--group-add=[] 运行容器的用户组。
-i,--interactive=true/flase 保持标准输入打开,默认为 false。
--ipc="" 容器 IPC 命令空间,可以为其他容器或主机。
--isolation="default" 容器使用的隔离机制。
--log-driver="json-file" 指定容器的日志启动类型,可以为 json-file、syslog、journald、gelf、fluentd、awslogs、splunk、etwlogs、gcplogs 或 none。
--log-opt=[] 传递给日志驱动的选项。
--net="bridge" 指定容器的网络模式,包括 bridge、none、其他容器内网络、host 的网络或者某个现有网络等。
--net-alias=[] 容器在网络中的别名。
-P,--publish-all=true/false 通过 NAT 机制将容器标记暴露的端口自动映射到本地的临时端口。
-p,--publish=[] 指定如何映射到本地主机端口,例如-p 11234-12234; 1234-2234 。
--pid=host 容器的 PID 命名空间。
--userns="" 启用 userns-remap 时配置用户命名空间的模式。
--uts=host 容器的 PID 命名空间。
--restart="no" 容器的重启策略,包括 no、on-faulure[: max-retry]、always、unless-stopped 等。
--rm=true\false 容器退出后是否自动删除,不能跟-d 同时使用。
--tmpfs=[] 挂载临时文件系统到容器。
-v,--volume[= HOST-DIR:] 挂载主机上的文件卷到容器内。
--volume-driver="" 挂载文件卷的驱动类型。
--volumes-from=[] 从其他容器挂载卷。
-w,--workdir="" 容器内的默认工作目录。
编者注:restart 参数。
参数 说明
on-faulure[: max-retry] 表示容器的退出状态码非 0 (非正常退出),自动重启。3 是自动重启的次数。超过 3 次则不重启。
always 表示容器退出时,docker 会总是自动重启这个容器。
unless-stopped 表示容器退出时总是重启,但是不考虑 docker 守护进程运行时就已经停止的容器。
no 默认值,容器退出时,docker 不自动重启容器。

3.2 Create 命令与容器环境和配置相关的选项

选项 说明
--add-host=[] 在容器内添加一个主机名到 IP 地址的映射关系(通过/etc/hosts 文件)
--device=[] 映射物理机上的设备到容器内。
--dns-search=[] DNS 搜索域。
--dns-opt=[] 自定义的 DNS 选项。
--dns=[] 自定义的 DNS 服务器。
-e,--env=[] 指定容器内环境变量。
--env-file=[] 从文件中读取环境变量到容器内。
-h,--hostname="" 指定容器内的主机名。
--ip="" 指定容器的 IPv4 地址。
--ip6="" 指定容器的 IPv6 地址。
--link=[name or id: alias ] 链接到其他容器。
--mac-address="" 指定容器的 Mac 地址。
--name="" 指定容器的别名。

3.3 Create 命令与容器资源限制和安全保护相关的选项

参数 说明
--blkio-weight=10~1000 容器读写块设备的 I/O 性能权重,默认为 0。
--blkio-weight-device=[DEVICE_NAME: WEIGHT ] 指定各个块设备的 I/O 性能权重。
--cpu-shares=0 允许容器使用 CPU 资源的相对权重,默认一个容器能用满一个核的 CPU。
--cap-add=[] 增加容器的 Linux 指定安全能力。
--cap-drop=[] 移除容器的 Linux 指定安全能力。
--cgroup-parent="" 容器 cgroups 限制的创建路径。
--cidfile="" 指定容器的进程 ID 号写到文件。
--cpu-period=0 限制容器在 CFS 调度器下的 CPU 占用时间片。
--cpuset-cpus="" 限制容器能使用那些 CPU 核心。
--cpuset-mem="" NUMA 架构下使用哪些核心的内存。
--cpu-quota=0 限制容器在 CFS 调度器下的 CPU 配额。
--device-read-bps=[] 挂载设备的读吞吐率(以 bps 为单位)限制。
--device-write-iops=[] 挂载设备的写速率(以每秒 i/o 次数为单位)限制。
--kernel-memory="" 限制容器内应用使用的内存,单位可以使 b、k、m 或 g。
--memory-reservation="" 当系统中内存过低是,容器会被强制限制内存到给定值,默认情况下等于内存限制值。
--memory-swap="LIMIT" 限制容器使用内存和交换区的总大小。
--oom-kil-disable=true/false 内存耗尽(Out-Of-=Memory)时是否杀死容器。
--oom-score-adj="" 调整容器的内存耗尽参数。
--pids-limit="" 限制容器的 pid 个数。
--privileged=true/false 是否给容器高权限,这意味着容器内应用将不受权限限制,一般不推荐。
--read-only=true/false 是否让容器内的文件系统只读。
--security-opt=[] 指定一些安全参数,包括权限、安全能力、apparmor 等。
--stop-signal=SIGTERM 指定停止容器的系统信息。
--shm-size="" /dev/shm 的大小。
--sig-proxy=true/false 是否代理收到的信号给应用,默认为 true,不能代理 SIGCHLD、SIGSTOP 和 SIGKILL 信号。
--memory-swappiness="0~100" 调整容器的内存交换区
-u,--user="" 指定在容器内指定命令的用户。
--ulimit=[] 通过 ulimit 来限制最大文件书、最大进程数等。

3.4 其他比较重要的选项还包括:

参数 说明
--label=[] 以键值对方式指定容器的标签信息;
label-file=[] 从文件中读取标签信息。

3.5 创建一个容器

$ docker create   -it   --name="kcp_centos" centos:7.2.1511